#include "preprocessor.h"
#include "row.h"
#include "avl.h"



int isspecial(char c);
int nextComma(char* buffer, int ant, int len);
int hasSpace(char*buffer, int init, int end);
void delWordSpace(char*buffer, int init, int end);
char* breakFive(char* word, int init);

node * preProcessor(int *word_counter, int *words_size, node **word5, node **word10, node **word15, node **word20, node **word25) {

    FILE * pFile;
    long lSize;
    char * pch = 0;
    char * buffer;
    size_t result;
    node *words = NULL;
    //   node *word5 = NULL;
    //   node *word10 = NULL;
    //   node *word15 = NULL;
    //   node *word20 = NULL;
    //   node *word25 = NULL;
    puts("\n\nAbrindo o arquivo texto....");
    pFile = fopen("palavras.txt", "rb");

    if (pFile == NULL) {
        fputs("File error", stderr);
        exit(1);
    }

    // obtain file size:
    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile);
    rewind(pFile);
    // allocate memory to contain the whole file:
    puts("Alocando memoria para o buffer principal....");
    buffer = (char*) malloc(sizeof (char)*lSize + 1);

    if (buffer == NULL) {
        fputs("Memory error", stderr);
        exit(2);
    }


    // copy the file into the buffer:
    puts("Copiando arquivo para o buffer principal....");
    result = fread(buffer, 1, lSize, pFile);

    if (result != lSize) {
        fputs("Reading error", stderr);
        exit(3);
    }

    /* the whole file is now loaded in the memory buffer. */
    // Close pFile by now
    fclose(pFile);
    int i, word = 0, wordtotal = 0;
    //Size of buffer
    size_t b = lSize;
    //To previne garbage
    buffer[lSize] = '\0';
    puts("Trocando pontuacao por \\n....");

    int ant = 0, next = 0;
    i = 1;


    puts("Removendo palavras formadas com espaço....");
    int len = strlen(buffer);
    while (next < len - 1) {
        next = nextComma(buffer, ant, len);

        if (hasSpace(buffer, ant, next))
            delWordSpace(buffer, ant, next);

        ant = next + 1;

        //       printf("next %d\n",next);
    }


    for (i = 0; i < b; i++) {
        if (buffer[i] == '-')
            buffer[i] = '{';
        if(isalpha(buffer[i]))
          buffer[i] = tolower(buffer[i]);
        if (isspecial(buffer[i]))
            buffer[i] = '\n';
        wordtotal++;
    }

    pFile = fopen("res_word_1.txt", "w");
    fwrite(buffer, 1, lSize, pFile);
    fclose(pFile);

    pFile = fopen("res_word_2.txt", "w");

    //     printf("%d",word);
    pch = strtok(buffer, "\n");
    word = 0;

    while (pch != NULL) {
      i=0;
        if (find(pch, words) == NULL) {

            len = strlen(pch);
            words = insert(pch, words);

            *word5 = insertSub(breakFive(pch, 0), pch, *word5, 0);
            if (len > 5)
                *word10 = insertSub(breakFive(pch, 5), pch, *word10, 1);
            if (len > 10)
                *word15 = insertSub(breakFive(pch, 10), pch, *word15, 2);
            if (len > 15)
                *word20 = insertSub(breakFive(pch, 15), pch, *word20, 3);
            if (len > 20)
              while(i*5 + 19 < len){
                *word25 = insertSub(breakFive(pch,i*5+20), pch, *word25, i+4);
                i++;
              }
            fprintf(pFile, "%s\n", pch);
            word++;
            words_size[strlen(pch)]++;

        }

        pch = strtok(NULL, "\n");
    }
    fclose(pFile);

    *word_counter = word;

    //   display_avl(*word5);




    return words;
}

char* breakFive(char* word, int init) {
    char* temp = (char*) malloc(sizeof (char)*6);
    strncpy(temp, word + init, 5);

    //   printf("palavra %s pedaco %s\n",word,temp);
    return temp;
}

int nextComma(char* buffer, int ant, int len) {
    int i;


    for (i = 0; (i + ant) < len; i++) {
        if (buffer[i + ant] == ',') {
            return i + ant;
        }



    }

    return i + ant;
}

int hasSpace(char*buffer, int init, int end) {
    int i;

    for (i = init; i < end; i++) {
        if (buffer[i] == ' ')
            return 1;
    }

    return 0;

}

void delWordSpace(char*buffer, int init, int end) {
    int i;

    for (i = init; i < end; i++)
        buffer[i] = '\n';

    return;
}

int isspecial(char c) {
    if (c == ',' || c == '\t' || c == '\v' || c == '\f' || c == '\r' || c == '\n' || c == '\\' || c == '/')
        return 1;

    else
        return 0;
}
